package cn.gxa.j229.bug.finance.controller.borrowing;

import cn.gxa.j229.bug.finance.BusinessException;
import cn.gxa.j229.bug.finance.pojo.Loan;
import cn.gxa.j229.bug.finance.pojo.Sign;
import cn.gxa.j229.bug.finance.service.borrowing.SignService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author FanJiang
 * @Date 2020/9/27
 * @Version 1.0
 */

@Controller
@RequestMapping("/sign")
public class SignController {

    @Resource
    SignService signService;

    /**
     * 标的上架页面接口，分页、模糊均在一次歌接口
     *
     * @param loan 传入一个loan对象的json例如：{"limit":1,"page":2}
     *             {"phone":"1","borrower":"测试","limit":1,"page":2}
     * @return 一个json
     */
    @PostMapping("/findPage")
    @ResponseBody
    public Map findPage(@RequestBody Loan loan){
        List<Map<String, Object>> byUnionPage = signService.findByUnionPage(loan);
        Integer counts = signService.countPage(loan);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("count", counts);
        jsonResult.setData(byUnionPage);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");

        return jsonResult.getValues();

    }

    @PostMapping("/insert")
    @ResponseBody
    public Map insert(@RequestBody Sign sign) throws BusinessException{
        if (sign == null || sign.equals("")){
            throw new BusinessException("50006","不能插入全为空的数据");
        }
        if(sign.getManners().equals("成交日")){
            sign.setManner(1);
        }
        if(sign.getManners().equals("T+1")){
            sign.setManner(2);
        }
        if(sign.getManners().equals("T+2")){
            sign.setManner(3);
        }
        if(sign.getManners().equals("成立计息")){
            sign.setManner(4);
        }
        if(sign.getDitchs().equals("PC")){
            sign.setDitch(1);
        }
        if(sign.getDitchs().equals("APP")){
            sign.setDitch(2);
        }
        int i = signService.updateSelective(sign);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData("成功上架" + i + "条标的");
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");

        return jsonResult.getValues();
    }

}
